
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_wan_android/arch/net/http_manager.dart';
import 'package:flutter_wan_android/arch/net/result_data.dart';
import 'package:flutter_wan_android/arch/util/date_util.dart';
import 'package:flutter_wan_android/common/api.dart';
import 'package:flutter_wan_android/event/event.dart';
import 'package:flutter_wan_android/event/event_bus.dart';
import 'package:flutter_wan_android/model/my_point.dart';
import 'package:flutter_wan_android/model/user_info.dart';
import 'package:flutter_wan_android/res/colors.dart';
import 'package:flutter_wan_android/res/theme.dart';
import 'package:flutter_wan_android/widget/refresh.dart';
import 'package:flutter_wan_android/widget/wan_appbar.dart';
import 'package:fluttertoast/fluttertoast.dart';

class MyPointPage extends StatefulWidget {
  @override
  _MyPointPageState createState() => _MyPointPageState();
}

class _MyPointPageState extends State<MyPointPage> {
  List<MyPoint> _points = [];

  EasyRefreshController _controller = EasyRefreshController();

  var _pageIndex = 0;

  var _totalCount = 0;

  @override
  void initState() {
    super.initState();
    _getTotal();
    _getMyPoints();

    EventBusUtil.getInstance().on<String>().listen((event) {
      if(event == WEvent.EVENT_LOGIN){
       _getTotal();
       _getMyPoints();
      }
    });
  }

  @override
  void dispose() {
    super.dispose();
    EventBusUtil.getInstance().destroy();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: WanAppbar(
        context,
        title: Text("我的积分"),
      ),
      body: Container(
        color: context.wTheme.background,
        child: Column(
          children: [
            Container(
              height: 140,
              color: wTheme.primary,
              width: double.infinity,
              child: Center(
                child: Text(
                  "${_totalCount}",
                  style: TextStyle(
                      fontSize: 56,
                      fontWeight: FontWeight.bold,
                      color: WColor.white),
                ),
              ),
            ),
            Expanded(
                flex: 1,
                child: EasyRefresh(
                  controller: _controller,
                  enableControlFinishLoad: true,
                  enableControlFinishRefresh: true,
                  header: refreshHeader,
                  footer: refreshFooter,
                  child: ListView.builder(
                    itemBuilder: (BuildContext context, int index) {
                      var item = _points[index];

                      return Container(
                        width: double.infinity,
                        padding: EdgeInsets.all(15),
                        child: Row(
                          children: [
                            Expanded(
                                flex: 1,
                                child: Container(
                                  child: Column(
                                    crossAxisAlignment:
                                        CrossAxisAlignment.start,
                                    children: [
                                      Text(
                                        "签到" + item.desc .split(",")[1] .replaceAll(" ", ""),
                                        style: TextStyle( fontSize: 16, color: wTheme.text1),
                                      ),
                                      Padding(padding: EdgeInsets.only(top: 5)),
                                      Text(
                                        DateUtil.format(item.date),
                                        style: TextStyle( fontSize: 12, color: WColor.gray1),
                                      )
                                    ],
                                  ),
                                )),
                            Text(
                              "+${item.coinCount}",
                              style: TextStyle(
                                fontSize: 16,
                                color: WColor.blue1,
                                fontWeight: FontWeight.bold,
                              ),
                            )
                          ],
                        ),
                      );
                    },
                    itemCount: _points.length,
                  ),
                  onRefresh: () async {
                    _pageIndex = 0;
                    _getMyPoints();
                  },
                  onLoad: () async {
                    _getMyPoints();
                  },
                ))
          ],
        ),
      ),
    );
  }

  _getMyPoints() async {
    ResultData result = await HttpManager.instance()
        .get("lg/coin/list/${_pageIndex}/json", withLoading: _pageIndex == 0);

    _controller.finishRefresh();
    bind(_controller, result);

    if (result.success) {
      setState(() {
        if (_pageIndex == 0) {
          _points = getMyPointList(result.data["datas"]);
        } else {
          _points.addAll(getMyPointList(result.data["datas"]));
        }
      });
      _pageIndex++;
    } else {
      Fluttertoast.showToast(msg: result.msg);
    }
  }


  _getTotal() async{
    ResultData result = await HttpManager.instance().get(Api.USER_INFO);
    if (result.success) {
      setState(() {
        var user = UserInfo.fromJson(result.data);
        _totalCount = user.coinCount ?? 0;
      });
    }
  }
}
